package com.zttek.videoShareProject.modules.web;


import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.videoShareProject.common.json.PageInfo;
import com.zttek.videoShareProject.common.json.Result;
import com.zttek.videoShareProject.common.json.UserJson;
import com.zttek.videoShareProject.modules.model.Comment;
import com.zttek.videoShareProject.modules.model.Keyword;
import com.zttek.videoShareProject.modules.model.User;
import com.zttek.videoShareProject.modules.service.ICommentService;
import com.zttek.videoShareProject.modules.service.IKeywordService;
import com.zttek.videoShareProject.modules.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.security.provider.MD2;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBY
 * @since 2018-07-03
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
    private IUserService iUserService;
	@Autowired
    private IKeywordService iKeywordService;
	@Autowired
    ICommentService iCommentService;

	@RequestMapping("userList")
    public ModelAndView userManagement(@RequestParam(value = "pn",defaultValue ="1")int pageNum,
                                       @RequestParam(value = "status",defaultValue = "1")Integer status) {


        ModelAndView modelAndView = new ModelAndView();
        Page<User> page=iUserService.getUserList(pageNum,1);
        PageInfo pageInfo=new PageInfo();
        pageInfo.setCurrentPage((long)(pageNum));
        pageInfo.setTotal((long)(page.getPages()));
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserJson> list2=iUserService.getUserJsonList(page);
        modelAndView.addObject("list",list2);
        modelAndView.setViewName("/user/userList");
        return modelAndView;
	}

    @ResponseBody
    @RequestMapping("addUser")
    public Result addUser(@RequestParam(value = "account")String account,

                      @RequestParam(value="email")String email,
                      @RequestParam(value ="password")String password,
                      @RequestParam(value ="checkPassword")String checkPassword,
                      @RequestParam(value="phone")String phone,
                      @RequestParam(value="optionsRadios")String sex
                    ){

        Result result=new Result();
        if(password.equals(checkPassword)) {
            User user = new User();
            user.setAccount(account);
            user.setEmail(email);
            user.setPassword(password);
            user.setTelephone(phone);
            user.setScore(0);
            user.setStatus(1);//默认是1，允许分享
            user.setUserType(0);//默认是普通用户
            user.setImageUrl("https://bpic.588ku.com/video_listen/588ku_pic/18/06/08/09ccdffb77dca9109acef41250b04cc6.jpg!/both/473x264/force/true");//设置默认用户头像
            System.out.println("a");
            Date date=new Date();
            System.out.println(date);
            user.setCreateDate(date);
            if (sex.equals("male")) {
                user.setSex(0);
            } else {
                user.setSex(1);
            }
            if(iUserService.insert(user)){
              result.setSuccess(true);
              result.setMsg("注册成功");
            }else{
               result.setSuccess(false);
               result.setMsg("账号名已被占用");
            }
        }else{
            result.setSuccess(false);
            result.setMsg("密码不一致");
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("updateUser")
    public Result updateUser(@RequestParam(value = "account")String account,

                          @RequestParam(value="email")String email,
                          @RequestParam(value ="password")String password,
                          @RequestParam(value ="checkPassword")String checkPassword,
                          @RequestParam(value="phone")String phone,
                          @RequestParam(value="optionsRadios")String sex
    ){

        Result result=new Result();
        System.out.println("65659");
        if(password.equals(checkPassword)) {
            User user = iUserService.selectUserByAccount(account);


            user.setEmail(email);
            user.setPassword(password);
            user.setTelephone(phone);

            System.out.println("c");



            if (sex.equals("male")) {
                user.setSex(0);
            } else {
                user.setSex(1);
            }
            if(iUserService.updateById(user)){

                result.setSuccess(true);
                result.setMsg("修改成功");
            }else{
                result.setSuccess(false);
                result.setMsg("账号名已被占用");
            }
        }else{
            result.setSuccess(false);
            result.setMsg("密码不一致");
        }
        return result;
    }

    @RequestMapping("login")
    @ResponseBody
    public Result login(@RequestParam(value = "account")String account,
                        @RequestParam(value="password") String password
                      ) {

        System.out.println(account);
        System.out.println(password);
        Result result=new Result();
        User user=iUserService.selectUserByAccount(account);
        if(user!=null){
            if(user.getPassword().equals(password)){
                result.setSuccess(true);
                result.add("user",user);
            }else{
                result.setSuccess(false);
                result.setMsg("密码错误");
            }
        }else{
            result.setSuccess(false);
            result.setMsg("账号错误");
        }
           return result;
}

    @RequestMapping("findUser")
    @ResponseBody
    public Result findUser(@RequestParam(value = "ID")String ID){
        System.out.println("a+");
        System.out.println(ID);
        Result result=new Result();
//        System.out.println(data);
        try {

            User user = iUserService.selectUserById(Long.parseLong(ID));
            System.out.println(user.toString());
            result.setSuccess(true);
            result.add("user",user);
        }catch (Exception ex){

        }


        return result;
    }

    @RequestMapping("test")
    public ModelAndView test()throws Exception{
        System.out.println("test");
        ModelAndView modelAndView=new ModelAndView();
        User user=iUserService.selectUserById(Long.parseLong("2"));
        modelAndView.addObject("account",user.getAccount());
        modelAndView.setViewName("/main/index");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("deleteUser")
    public Result deleteUser(@RequestParam(value = "ID")Long ID) throws Exception {
        System.out.println(ID);
        Result result=new Result();
        User user=iUserService.selectUserById(ID);
        user.setStatus(0);
        if(iUserService.updateById(user)){
            result.setSuccess(true);
            result.setMsg("删除用户成功");
        }else{
            result.setSuccess(false);
            result.setMsg("删除用户失败");
        }
        return result;
    }

    @RequestMapping("findUserByAccount")
    public ModelAndView findUserByAccount(@RequestParam(value = "name")String name){
        ModelAndView modelAndView = new ModelAndView();
        Page<User> page=iUserService.findUserByAccount(name);
        PageInfo pageInfo=new PageInfo();
        pageInfo.setCurrentPage((long)(1));
        pageInfo.setTotal((long)(page.getPages()));
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserJson> list2=iUserService.getUserJsonList(page);
        modelAndView.addObject("list",list2);
        modelAndView.setViewName("/user/userList");
        return modelAndView;
    }
}
